package cn.xspace.library.util;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

import com.xspace.android.xafcommon.appliction.BaseApplication;

import java.io.IOException;

/**
 * Created by liuchen on 2016/12/2.
 */

public class DrawableUtils {

    public static GradientDrawable getDrawable(Context context, int backgroundColor, int rgb, float radius, int type) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(backgroundColor);//设置颜色
        gradientDrawable.setShape(type);
        gradientDrawable.setCornerRadius(radius);//设置圆角的半径
        gradientDrawable.setStroke(DensityUtil.dip2px(context, 1), rgb);//描边
        return gradientDrawable;
    }

//    public static ShapeDrawable getShapeDrawable(){
//        ShapeDrawable shapeDrawable = new ShapeDrawable();
//        shapeDrawable.setShape(ShapeDrawable.);
//    }

    public static StateListDrawable getSelector(Drawable normalDrawable, Drawable pressDrawable) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        //给当前的颜色选择器添加选中图片指向状态，未选中图片指向状态
        stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, pressDrawable);
        stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, normalDrawable);
        //设置默认状态
        stateListDrawable.addState(new int[]{}, normalDrawable);
        return stateListDrawable;
    }

    /**
     * 对TextView设置不同状态时其文字颜色。
     */
    private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {
        int[] colors = new int[]{pressed, focused, normal, focused, unable, normal};
        int[][] states = new int[6][];
        states[0] = new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled};
        states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
        states[2] = new int[]{android.R.attr.state_enabled};
        states[3] = new int[]{android.R.attr.state_focused};
        states[4] = new int[]{android.R.attr.state_window_focused};
        states[5] = new int[]{};
        ColorStateList colorList = new ColorStateList(states, colors);
        return colorList;
    }


    public static StateListDrawable setSeletedState(Drawable normalDrawable, Drawable pressDrawable) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_selected}, pressDrawable);
        //设置默认状态
        stateListDrawable.addState(new int[]{}, normalDrawable);
        return stateListDrawable;
    }

    public static Bitmap getBitmapbyAssentsName(String resName) {
        try {
            return BitmapFactory.decodeStream(BaseApplication.mInstance.getAssets().open(BuildConfig.ASSENT_ICONS_NAME + resName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Drawable getDrawablepbyAssentsName(String resName) {
        try {
            return Drawable.createFromStream(BaseApplication.mInstance.getAssets().open(BuildConfig.ASSENT_ICONS_NAME + resName), resName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


}
